package terra;

import java.sql.SQLException;

import terra.database.DatabaseFactory;
import terra.managers.MapManager;
import terra.model.World;
import terra.model.map.Map;
import terra.network.Connections;
import terra.network.PlayerHandler;

public class Shutdown implements Runnable {

	@Override
	public void run() {
		System.out.println("shutdown started. Save all data.");
		World.getInstance().thread.interrupt();
		for(PlayerHandler ph:  Connections.connections.values())
			ph.getChannel().disconnect();
		for (Map m : MapManager.getInstance().getMaps().values())
			m.save();
		MapManager.getInstance().saveNpcs();
		try {
			DatabaseFactory.getInstance().shutdown();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println("shutdown! data saved");
	}

}
